﻿using System;
using System.Diagnostics;
using System.Linq;
using System.Windows.Forms;
using Syncfusion.XlsIO;

using PMngr.Data;

namespace PMngr.Main
{
    public partial class UnitTemplate : BaseControl
    {
        private IRepositoryFactory repositoryFactory;
        private string templateFile;

        public UnitTemplate()
        {
            InitializeComponent();
        }

        public UnitTemplate(IRepositoryFactory repositoryFactory)
            : this()
        {
            this.repositoryFactory = repositoryFactory;
        }

        private void BtnProductClick(object sender, EventArgs e)
        {
            var saveFileDialog = new SaveFileDialog
                                     {
                                         Filter = @"Files(*.xls)|*.xls",
                                         AddExtension = true,
                                         DefaultExt = ".xls"
                                     };

            if (saveFileDialog.ShowDialog() == DialogResult.OK && saveFileDialog.CheckPathExists)
            {
                templateFile = saveFileDialog.FileName;

                GenerateTemplate(templateFile);

                LogTrace(String.Format(Utilities.TemplateCompleted, templateFile));

                if (MessageFactory.MessageBoxShow(Utilities.OpenExcelConfirmed) == DialogResult.Yes)
                {
                    var proc = new Process {StartInfo = {FileName = saveFileDialog.FileName}};
                    proc.Start();
                }
            }
        }      

        private void GenerateTemplate(string file)
        {
            //Step 1 : Instantiate the spreadsheet creation engine.
            var excelEngine = new ExcelEngine();
            //Step 2 : Instantiate the excel application object.
            IApplication application = excelEngine.Excel;
            var template = Application.StartupPath + @".\docs\unitTemplate.xls";
            IWorkbook workbook = application.Workbooks.Open(template);
            IWorksheet sheet = workbook.Worksheets[0];
            #region Validation
            // 单位性质
            IDataValidation xlValidator = sheet.Range[5, 2, 1000, 2].DataValidation;
            xlValidator.ListOfValues = GetItems("UnitKind");
            xlValidator.PromptBoxText = Utilities.UnitKindSelected;
            xlValidator.IsPromptBoxVisible = true;
            xlValidator.ShowPromptBox = true;
            #endregion
            //Saving the workbook to disk.
            workbook.SaveAs(file);
            //Close the workbook.
            workbook.Close();
            //No exception will be thrown if there are unsaved workbooks.
            excelEngine.ThrowNotSavedOnDestroy = false;
            excelEngine.Dispose(); 
        }

        private string[] GetItems(string type)
        {
            using (var repository = repositoryFactory.CreateRepository())
            {
                IQueryable<string> items =
                    repository.Find<ItemBase>(i => i.Type == type && i.IsValid == true).OrderBy(i => i.Order).Select(
                        i => i.Name);
                return items.ToArray();
            }
        }
    }
}
